package com.woniuxy.consumer;

import com.rabbitmq.client.Channel;
import com.woniuxy.entity.Orders;
import com.woniuxy.mapper.OrdersMapper;
import com.woniuxy.util.RabbitMQConst;
import com.woniuxy.util.RabbitMQMessageHelper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ShoppingCartConsumer {
    @Resource
    private OrdersMapper ordersMapper;

    /**
     * <p>向数据库中写入来自用户端小程序购物车页面结算按钮发送的订单信息。</p>
     * @param orders
     * @param channel
     * @param message
     */
    @RabbitListener(queues = RabbitMQConst.ADD_ORDERS_QUEUE)
    public void receiveAddOrdersMessage(Orders orders, Channel channel, Message message) {
        RabbitMQMessageHelper.processMessageWithAck(channel, message, msg -> ordersMapper.insertOrdersFromCart(orders));
    }
}
